package com.xtecher.app.column.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xtecher.app.R;
import com.xtecher.app.column.bean.ColumnListResponse;
import com.xtecher.app.widget.OnItemClickListener;

import java.util.List;

/**
 * Created by Luhao on 2017/2/22.
 */

public class ColumnListAdapter extends RecyclerView.Adapter {
    private final Context context;
    private final List<ColumnListResponse> responseList;
    private OnItemClickListener onItemClickListener;

    public ColumnListAdapter(Context context, List<ColumnListResponse> responseList) {
        this.context = context;
        this.responseList = responseList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ColumnHolder(LayoutInflater.from(context).inflate(R.layout.item_column, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ColumnHolder columnHolder = (ColumnHolder) holder;
        columnHolder.tvColumn.setText(responseList.get(position).getTitle());
        Glide.with(context)
                .load(responseList.get(position).getCover_pic())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .error(R.drawable.list_cover)
                .placeholder(R.drawable.list_cover)
                .into(columnHolder.imgColumn);
        columnHolder.reColumn.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(columnHolder.reColumn, columnHolder.getAdapterPosition());
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return responseList != null ? responseList.size() : 0;
    }

    private class ColumnHolder extends RecyclerView.ViewHolder {

        private RelativeLayout reColumn;
        private ImageView imgColumn;
        private TextView tvColumn;

        public ColumnHolder(View view) {
            super(view);

            reColumn = (RelativeLayout) view.findViewById(R.id.re_column);
            imgColumn = (ImageView) view.findViewById(R.id.img_column);
            tvColumn = (TextView) view.findViewById(R.id.tv_column);

        }
    }
}
